package leetcode;

import java.util.*;

public class Lc2542_2 {

    public long maxScore(int[] nums1, int[] nums2, int k) {

        if (k == nums1.length) {
            int sum = Arrays.stream(nums1).sum();
            int min = Arrays.stream(nums2).min().getAsInt();
            return sum * min;
        }

        ArrayList<int[]> arr1 = new ArrayList<>();
        ArrayList<int[]> arr2 = new ArrayList<>();
        for (int i = 0; i < nums1.length; i++) {
            arr1.add(new int[]{nums1[i], i});
            arr2.add(new int[]{nums2[i], i});
        }

        Collections.sort(arr1, ((o1, o2) -> o2[0] - o1[0])); //从大到小
        Collections.sort(arr2, ((o1, o2) -> o1[0] - o2[0])); //从小到大

        long ans = 0;

        for (int i = 0; i < arr2.size(); i++) {
            int idx = arr2.get(i)[1];
            long sum = 0, cnt = 0, minNum2 = Integer.MAX_VALUE;
            for (int j = 0; j < arr1.size(); j++) {
                int index = arr1.get(j)[1];
                if (index != idx) {
                    sum += arr1.get(j)[0];
                    cnt++;
                    minNum2 = Math.min(minNum2, nums2[index]);
                    if (cnt == k) break;
                }
            }
            ans = Math.max(ans, sum * minNum2);
        }
        return ans;
    }

    public static void main(String[] args) {
        Lc2542_2 lc2542_2 = new Lc2542_2();
        int[] n = {1,2,1};
        int[] m = {1,0,4};
        int k = 1;
        System.out.println(lc2542_2.maxScore(n,m,k));
    }

}
